<?php

/**
 * ClassName:帖子操作类
 * @auditor LeiJunXiang
 * @time 2024/10/28
 */

namespace app\modelOpreate\invite;

use app\model\invite\BrowsingHistory;
use app\modelOpreate\ModelOpeBase;
use app\modelOpreate\user\ForumUserOpe;
use InvalidArgumentException;
use think\facade\Db;


class Invite extends ModelOpeBase
{
    const TOOL_INVITE_OPE = "Invite";


    /**
     * (true/false)插入帖子（帖子数组数据）
     * @return void
     */
    public function insertInvite($arr)
    {
        $db = $this->getDb();
        $flag = $db->insert($arr);
        return $flag;
    }

    /**
     * 查询个人帖子
     */
    public function selectPersonInvite($userId)
    {
        $data = Db::table($this->table)
            ->where('publishId', $userId)
            ->order('publishTime', 'desc')
            ->withoutField("_id,hotWeight,content,count")
            ->select();
        return $data;
    }


    /**
     * 根据帖子对象key查询帖子信息
     * @param $objKey
     */
    public function selectInviteByObjKey($objKey)
    {
        $data = Db::table($this->table)
            ->where("objKey", "=", $objKey)
            ->find();
        return $data;
    }

    /**
     * 根据帖子对象key查询指定的帖子信息
     * @return void
     */
    public function selectPostDetailsByObjKey($postObjKey)
    {
        $data = Db::table($this->table)
            ->where('objKey', $postObjKey)
            ->field('title,content,pictureLinkList')
            ->find();
        return $data;
    }

    /**
     * (帖子队列)查询已发布的最热帖子(查询个数，需剔除的帖子对象key)
     */
    public function selectHotInvite($num, $excludeIds)
    {

        //查询并剔除已浏览的帖子
        $inviteList = Db::table($this->table)
            ->withoutField("_id,hotWeight,content,count")
            ->whereNotIn('objKey', $excludeIds)
            ->where('status', 1)
            ->order('hotWeight', 'desc')
            ->limit($num)->select()->toArray();

        return $inviteList;
    }

    /**
     * (帖子队列)查询已发布的最新帖子(查询个数,需剔除的帖子对象key)
     */
    public function selectNewInvite($num, $excludeIds)
    {
        $newList = Db::table($this->table)
            ->withoutField("_id,hotWeight,content,count")
            ->whereNotIn('objKey', $excludeIds)
            ->order('publishTime', 'desc')
            ->where('status', 1)
            ->limit($num)->select()->toArray();
        return $newList;
    }

    public function selectTest()
    {
        $db = \think\facade\Db::connect('mongo');
        $command = [
            'aggregate' => '表名',
            'pipeline' => [
                ['$group' => ['_id' => '$聚合字段名', 'sum' => ['$sum' => 1]]],
            ],
            'cursor' => new \stdClass()
        ];
        $r = $db->cmd($command);
        halt($r);

    }

}